<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/

//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

if($class_pi->checkUserActionRight(14)!=true){
	echo '<script> alert(\''._pi_lang_no_move_item.'\'); window.history.go(-1); </script>';
	exit();
}

//get data of item
$item_id = $class_pi->get_var('item_id', '' );	
$class_pi->db->setQuery("SELECT catid, title FROM #__content WHERE id=$item_id LIMIT 1");
$rows = $class_pi->db->loadObjectList();
$row = $rows[0];

$old_cat_id = $row->catid;
$title = $row->title;

//get itemtype from item_index
$class_pi->db->setQuery("SELECT itemtype FROM #__pi_item_index WHERE item_id=$item_id LIMIT 1");
$rows = $class_pi->db->loadObjectList();
$row = $rows[0];
$item_type = $row->itemtype;
if(!$item_type){
	$item_type = 'text';
}
	
echo "<script src=\"components/com_pi_pages_and_items/javascript/dtree.js\" language=\"JavaScript\" type=\"text/javascript\"></script>\n";
echo "<script src=\"../includes/js/overlib_mini.js\" language=\"JavaScript\" type=\"text/javascript\"></script>\n";
	
//give headers in Joomla 1.5 a bit more spunk
$class_pi->spunk_up_headers_1_5();

?>
<script language="JavaScript" type="text/javascript">
<!--

function select_parent(parent_id){
	document.getElementById('new_parent_id').value = parent_id;
}

function submitbutton(pressbutton) {
	if (pressbutton == 'cancel') {
		document.location.href = 'index2.php?option=com_pi_pages_and_items&task=item&sub_task=edit&pageId='+<?php echo $pageId; ?>+'&itemId='+<?php echo $item_id; ?>;		
	}
	if (pressbutton == 'item_move_save') {	
		if (document.adminForm.new_parent_id.value == '' ) {			
			alert( '<?php echo _pi_lang_need_select_page; ?>' );
			return;
		} else {
			submitform(pressbutton);			
		}
	}
}


-->
</script>

<table cellspacing="0" cellpadding="0" border="0" width="100%">
  <tr>
    <td valign="top" width="25%"><?php
		$class_pi->pages();
		?></td> 
    <td valign="top">
		<form name="adminForm" method="post" action="">
			<input type="hidden" name="option" value="com_pi_pages_and_items" />
			<input type="hidden" name="task" value="item_move_save" />				
			<input type="hidden" name="pageId" value="<?php echo $pageId; ?>">
			<input type="hidden" name="item_id" value="<?php echo $item_id; ?>">			
			<input type="hidden" name="old_cat_id" value="<?php echo $old_cat_id; ?>" />
			<input type="hidden" name="new_parent_id" id="new_parent_id" value="" />
							
		 	<div id="pi_breadcrumb">
				<a href="index2.php?option=com_pi_pages_and_items&amp;task=page&amp;sub_task=edit&amp;pageId=<?php echo $pageId; ?>"><?php echo _pi_lang_page; ?></a> >  <a href="index2.php?option=com_pi_pages_and_items&amp;task=item&amp;sub_task=edit&amp;pageId=<?php echo $pageId; ?>&amp;itemId=<?php echo $item_id; ?>"><?php echo _pi_lang_item.' ['.$class_pi->translate_item_type($item_type).']'; ?></a> > <?php echo _pi_lang_itemmove; ?>
			</div>
		   <table class="adminform">
			<tr>
				<th>
					 <?php echo _pi_lang_move_item; ?>: "<?php echo $title; ?>"
				</th>
			</tr>
				<tr>
					<td>						
						<p><?php echo _pi_lang_select_page; ?>.</p>						 
					</td>
				</tr>
				<tr>
					<td>
						<?php
						
						//see how many loops we need
						$loops = count($class_pi->menutypes);
						
						
						//loop menutypes
						for($m = 0; $m < $loops; $m++){								
							echo '<div class="dtree">';	
							echo '<p><a href="javascript: pages_tree'.$m.'.openAll();">'._pi_lang_open_all.'</a> | <a href="javascript: pages_tree'.$m.'.closeAll();">'._pi_lang_close_all.'</a></p>';
						
							//open javascript
							echo "<script type=\"text/javascript\"  type=\"text/javascript\">\n";
							echo "<!--\n";
							echo "pages_tree$m = new dTree('pages_tree$m');\n";						
							echo "pages_tree$m.add(0,-1,'";
							echo $class_pi->get_menutype_title($class_pi->menutypes[$m]);							
							echo "','','','','','',true);\n";
						
							//make javascript-array from main-menu-items
							foreach($class_pi->menuitems as $row){
								//check section access, only get all data if we realy need to
								if($class_pi->pi_config['use_user_access_component'] && $class_pi->user_access_config['active_sections'] && $class_pi->user_type!='Super Administrator'){
									//get category id of menuitem	
									if( defined('_JEXEC') ){
										//joomla 1.5
										$cat_id = str_replace('index.php?option=com_content&view=category&layout=blog&id=','',$row->link);
									}else{
										//joomla 1.0.x
										$cat_id = $row->componentid;	
									}	
									
									//get the section which the category belongs to	
									$class_pi->db->setQuery("SELECT id, section, name FROM #__categories WHERE id='$cat_id'");//name?
									$section_rows = $class_pi->db->loadObjectList();
									$section_row = $section_rows[0];
									$section_id = $section_row->section;								
									
								}else{
									$section_id = '';
								}								
								
								$show_item = false;
								if($row->menutype==$class_pi->menutypes[$m]){
									$show_item = true;
								}								
								if($show_item){
									echo "pages_tree$m.add(".$row->id.",".$row->parent.",'".(addslashes($row->name))."','";
									//if content page (content category blog) and not in the category it came from, make selectable link	
									if(((strstr($row->link, 'index.php?option=com_content&view=category&layout=blog') && $row->type!='url') || $row->type=='content_blog_category') && $row->id!=$pageId && $class_pi->check_section_access($section_id)){													
										echo 'javascript: select_parent('.$row->id.');';									
									}
									if(((strstr($row->link, 'index.php?option=com_content&view=category&layout=blog') && $row->type=='url') || !strstr($row->link, 'index.php?option=com_content&view=category&layout=blog')) && $row->type!='content_blog_category'){
										echo "','','','components/com_pi_pages_and_items/images/link.gif','components/com_pi_pages_and_items/images/link.gif";		
									}else{
										echo "','','','components/com_pi_pages_and_items/images/page.gif','components/com_pi_pages_and_items/images/page.gif";		
									}
									echo "');\n";
								}	
							}
													   
							echo "document.write(pages_tree$m);\n";						
						
							//close javascript
							echo "//-->\n";
							echo "</script>\n";
							echo '</div>';
						
						}//end loops menutypes
						?>									
					</td>
				</tr>
			</table>
		</form>
	</td>
  </tr>
</table>
<?php
	$class_pi->display_footer();
?>	
		